﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.工具区域的扩展类;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Tools
{
    /// <summary>
    /// 创建一个旋转矩形
    /// </summary>
    public class GenRectangle2
    {
        /// <summary>
        /// 该工具的线段对象
        /// </summary>
        HDrawingObjectEx line;

        /// <summary>
        /// 控件输入框
        /// </summary>
        public NumericUpDown XUpdown, YUpdown, PhiUpdown, Length1Updown, Length2Updown;
        public void DrawRectangle2(HWindow HWindowHandle, double IntputX, double InputY, double Phi, double Length1, double Length2,
            string Line_Color, NumericUpDown XUpdown = null, NumericUpDown YUpdown = null, NumericUpDown PhiUpdown = null, NumericUpDown Length1Updown = null, NumericUpDown Length2Updown = null)
        {
            this.XUpdown = XUpdown;
            this.YUpdown = YUpdown;
            this.PhiUpdown = PhiUpdown;
            this.Length1Updown = Length1Updown;
            this.Length2Updown = Length2Updown; 
            //清除矩形2
            ClearObject(HWindowHandle);
            //定义绘制线的开始坐标与结束坐标
            var lineTuple = new HTuple[] { IntputX, InputY, Phi, Length1, Length2 };
            //创建矩形的对象
            HDrawingObject lineObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, lineTuple);

            //设置矩形的颜色
            try
            {
                lineObj.SetDrawingObjectParams("color", Line_Color.ToLower());
            }
            catch
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText("该线条不支持此颜色以为你选择默认颜色!\r\n");
                lineObj.SetDrawingObjectParams("color", "red");
            }
            //矩形拖动触发事件注册
            lineObj.OnDrag(LineOnDrag);
            //线段长度修改触发事件注册
            lineObj.OnResize(LineOnResize);

            //永久保留该对象,不然会被垃圾回收机制回收
            line = new HDrawingObjectEx
            {
                HDrawingObject = lineObj,
                hTuples = lineTuple
            };
            //增加这个对象到指定Halcon窗口
            HWindowHandle.AttachDrawingObjectToWindow(lineObj);
        }

        /// <summary>
        /// 线段拖动触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void LineOnDrag(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecData(drawid);
        }
        /// <summary>
        /// 线段长度修改触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void LineOnResize(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecData(drawid);
        }
        /// <summary>
        /// 线段拖动修改长度方法
        /// </summary>
        /// <param name="drawid"></param>
        public void UpdataDrawingObjecData(HDrawingObject drawid)
        {
            try
            {
                if (XUpdown == null) return;
                //创建一个要得到对象属性的元组
                var attTuple = new HTuple("row", "column", "phi", "length1", "length2");
                //获取对应的属性
                var valueTuple = drawid.GetDrawingObjectParams(attTuple);
                //对对应的属性进行封装一下,后面好加入到集合中
                var attValues = new HTuple[] { valueTuple[0], valueTuple[1], valueTuple[2], valueTuple[3], valueTuple[4] };
                //更新数据
                line.hTuples = attValues;

                XUpdown.Value = decimal.Parse(valueTuple[0].D.ToString());
                YUpdown.Value = decimal.Parse(valueTuple[1].D.ToString());
                //这里弧度会失败

                if (decimal.TryParse(valueTuple[2].D.ToString(), out decimal result))
                {
                    this.PhiUpdown.Value = result;
                }
                Length1Updown.Value = decimal.Parse(valueTuple[3].D.ToString());
                Length2Updown.Value = decimal.Parse(valueTuple[4].D.ToString());
            }
            catch
            {

            }
        }
        /// <summary>
        /// 清除对象
        /// </summary>
        public void ClearObject(HWindow HWindowHandle)
        {
            if (line == null) return;
            var hobject = line.HDrawingObject;
            if (hobject == null) return;
            HWindowHandle.DetachDrawingObjectFromWindow(hobject);
            line = null;
            hobject.Dispose();
        }

        //获取位置信息
        public void GetPoint(out HDrawingObjectEx regionObj) {
            regionObj = line;
        }

        /// <summary>
        /// 移动
        /// </summary>
        /// <param name="HWindowHandle"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="raduis"></param>
        /// <param name="Line_Color"></param>
        public void Move(HWindow HWindowHandle, double x, double y, double phi, double length1,double length2,string Line_Color)
        {
            return;
            ClearObject(HWindowHandle);
            //定义绘制线的开始坐标与结束坐标
            var lineTuple = new HTuple[] { x, y, phi ,length1,length2};
            //创建线的对象
            var lineObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, lineTuple);
            //线段拖动触发事件注册
            lineObj.OnDrag(LineOnDrag);
            //线段长度修改触发事件注册
            lineObj.OnResize(LineOnResize);

            //设置线的颜色
            try
            {
                lineObj.SetDrawingObjectParams("color", Line_Color.ToLower());
            }
            catch
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText("该线条不支持此颜色以为你选择默认颜色!\r\n");
                lineObj.SetDrawingObjectParams("color", "red");
            }

            //永久保留该对象,不然会被垃圾回收机制回收
            line = new HDrawingObjectEx
            {
                HDrawingObject = lineObj,
                hTuples = lineTuple
            };
            //增加这个对象到指定Halcon窗口
            HWindowHandle.AttachDrawingObjectToWindow(lineObj);
        }
    }
}
